package com.project.server.service.estates.impl;

import com.project.common.core.domain.BaseServer;
import com.project.common.exception.CustomException;
import com.project.common.utils.StringUtils;
import com.project.server.domain.estates.TPayRecord;
import com.project.server.mapper.estates.TPayRecordMapper;
import com.project.server.service.estates.ITPayRecordService;
import io.netty.util.internal.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

;

/**
 * 缴费记录Service业务层处理
 *
 * @author project
 */
@Service
public class TPayRecordServiceImpl extends BaseServer implements ITPayRecordService {

    private static final Logger log = LoggerFactory.getLogger(TPayRecordServiceImpl.class);

    @Resource
    private TPayRecordMapper tPayRecordMapper;

    /**
     * 查询缴费记录
     *
     * @param payId 缴费记录ID
     * @return 缴费记录
     */
    @Override
    public TPayRecord selectTPayRecordById(Long payId) {
        return tPayRecordMapper.selectTPayRecordById(payId);
    }

    /**
     * 根据缴费单号查询缴费记录
     *
     * @param payOrder 缴费单号
     * @return 缴费记录
     */
    @Override
    public TPayRecord selectTPayRecordByPayOrder(String payOrder) {
        return tPayRecordMapper.selectTPayRecordByPayOrder(payOrder);
    }

    /**
     * 查询缴费记录列表
     *
     * @param tPayRecord 缴费记录
     * @return 缴费记录
     */
    @Override
    public List<TPayRecord> selectTPayRecordList(TPayRecord tPayRecord) {
        Date payTime = tPayRecord.getPayTime();
        tPayRecord.setPayTime(payTime);
        return tPayRecordMapper.selectTPayRecordList(tPayRecord);
    }

    /**
     * 根据userId查询缴费记录列表
     *
     * @param tPayRecord 缴费记录
     * @return 缴费记录
     */
    @Override
    public List<TPayRecord> selectTPayRecordByUserId(TPayRecord tPayRecord) {
        return tPayRecordMapper.selectTPayRecordByUserId(tPayRecord);
    }

    /**
     * 新增缴费记录
     *
     * @param tPayRecord 缴费记录
     * @return 结果
     */
    @Override
    public int insertTPayRecord(TPayRecord tPayRecord) {
        if("2".equals(tPayRecord.getPayStatus())){
            tPayRecord.setUnpaid(tPayRecord.getPay());
        }
        super.insert(tPayRecord);
        return tPayRecordMapper.insertTPayRecord(tPayRecord);
    }

    /**
     * 修改缴费记录
     *
     * @param tPayRecord 缴费记录
     * @return 结果
     */
    @Override
    public int updateTPayRecord(TPayRecord tPayRecord) {
        super.update(tPayRecord);
        return tPayRecordMapper.updateTPayRecord(tPayRecord);
    }

    /**
     * 批量删除缴费记录
     *
     * @param payIds 需要删除的缴费记录ID
     * @return 结果
     */
    @Override
    public int deleteTPayRecordByIds(Long[] payIds) {
        return tPayRecordMapper.deleteTPayRecordByIds(payIds);
    }

    /**
     * 删除缴费记录信息
     *
     * @param payId 缴费记录ID
     * @return 结果
     */
    @Override
    public int deleteTPayRecordById(Long payId) {
        return tPayRecordMapper.deleteTPayRecordById(payId);
    }

    /**
     * 导入缴费记录
     *
     * @param payRecordList   缴费记录列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName        操作用户
     * @return
     */
    @Override
    public String importPayRecord(List<TPayRecord> payRecordList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(payRecordList) || payRecordList.size() == 0) {
            throw new CustomException("导入缴费记录数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TPayRecord payRecord : payRecordList) {
            try {
                //验证是否存在这个缴费记录
                TPayRecord t = tPayRecordMapper.selectTPayRecordByPayOrder(payRecord.getPayOrder());
                if (StringUtils.isNull(t)) {
                    payRecord.setCreateBy(operName);
                    this.insertTPayRecord(payRecord);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、缴费单号" + payRecord.getPayOrder() + "导入成功");
                } else if (isUpdateSupport) {
                    payRecord.setPayId(t.getPayId());
                    payRecord.setPayOrder(t.getPayOrder());
                    payRecord.setUpdateBy(operName);
                    this.updateTPayRecord(payRecord);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、缴费单号" + payRecord.getPayOrder() + "更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、缴费单号" + payRecord.getPayOrder() + "已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、缴费单号" + payRecord.getPayOrder() + "导入失败: ";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！ 共" + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}

